#!/bin/bash
# description: Install tigervnc server for UOS desktop professional
# version: 0.0.1
# author: ZhangChiQian<zhangchiqian@uniontech.com>
# date: 2023-06-01

echo "+------------------------------------------+"
echo "|   开始安装tigervnc-scraping-server		   "
echo "+------------------------------------------+"


if dpkg -l tigervnc-common &>/dev/null && dpkg -l tigervnc-scraping-server &>/dev/null;then
    echo "tigervnc-scraping-server已经安装"
else
    echo "正在安装tigervnc ..."        
    sudo apt install -y tigervnc-common tigervnc-scraping-server
fi

if [ $? -ne 0 ];then
    echo "安装tigervnc-scraping-server失败！"
    exit -1
fi

echo "设置x0vncserver自启动服务"
sudo tee /etc/systemd/system/x0vncserver.service <<-'EOF'
[Unit]
Description=Remote desktop service (VNC) for :0 display
Requires=display-manager.service
After=network-online.target
After=display-manager.service

[Service]
Type=simple
Environment=HOME=/root
Environment=XAUTHORITY=/var/run/lightdm/root/:0
ExecStart=x0vncserver -display :0 -SecurityTypes None
Restart=on-failure
RestartSec=500ms

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable x0vncserver --now

if [ $? -eq 0 ];then
    echo -e "\033[40;37m 安装配置tigervnc-scraping-server完成，按回车键退出 \033[0m"
    read i
    exit 0
fi
